home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Ebooks / Thinking in C++ V2 / C19 / applySequence.h < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-25  |  1.0 KB  |  40 lines

  1. //: C19:applySequence.h
  2. // From Thinking in C++, 2nd Edition
  3. // Available at http://www.BruceEckel.com
  4. // (c) Bruce Eckel 1999
  5. // Copyright notice in Copyright.txt
  6. // Apply a function to an STL sequence container
  7.  
  8. // 0 arguments, any type of return value:
  9. template<class Seq, class T, class R>
  10. void apply(Seq& sq, R (T::*f)()) {
  11.   typename Seq::iterator it = sq.begin();
  12.   while(it != sq.end()) {
  13.     ((*it)->*f)();
  14.     it++;
  15.   }
  16. }
  17.  
  18. // 1 argument, any type of return value:
  19. template<class Seq, class T, class R, class A>
  20. void apply(Seq& sq, R(T::*f)(A), A a) {
  21.   typename Seq::iterator it = sq.begin();
  22.   while(it != sq.end()) {
  23.     ((*it)->*f)(a);
  24.     it++;
  25.   }
  26. }
  27.  
  28. // 2 arguments, any type of return value:
  29. template<class Seq, class T, class R, 
  30.          class A1, class A2>
  31. void apply(Seq& sq, R(T::*f)(A1, A2),
  32.     A1 a1, A2 a2) {
  33.   typename Seq::iterator it = sq.begin();
  34.   while(it != sq.end()) {
  35.     ((*it)->*f)(a1, a2);
  36.     it++;
  37.   }
  38. }
  39. // Etc., to handle maximum likely arguments ///:~
  40.